package com.swgk.core;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;

import com.alibaba.android.arouter.launcher.ARouter;
import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.Settings;
import com.orm.SugarContext;
import com.swgk.core.base.api.BaseApiSourceImpl;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.base.di.AppModule;
import com.swgk.core.base.di.DaggerAppComponent;
import com.swgk.core.base.di.NetworkModule;
import com.swgk.core.base.model.preference.BasePreferenceSource;
import com.swgk.core.exception.CrashHandlerUtil;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.screenadaptation.Density;
import com.swgk.core.util.LogUtil;
import com.swgk.core.util.MLog;

public class BaseAppHelper {
    @SuppressLint("StaticFieldLeak")
    private static Application instance;
    private AppComponent appComponent;
    private ActivityLifeCycle lifecycleCallbacks;

    @SuppressLint("StaticFieldLeak")
    private static BaseAppHelper sUtils;

    public static BaseAppHelper get() {
        if (sUtils == null) {
            sUtils = new BaseAppHelper();
        }
        return sUtils;
    }

    public Activity getActivity() {
        if (lifecycleCallbacks != null) {
            return lifecycleCallbacks.getActivity();
        }
        return null;
    }

    public Application getInstance() {
        return instance;
    }

    public <T> void onCreate(Application application, Class<T> preferenceSourceClass, BaseApiSourceImpl baseApiSource, String baseUrl) {
        instance = application;
        LogUtil.init(MLog.DEBUG_LOG, "IMLog");//LogUtils.init(AppUtils.isDebug(),"IMLog");
        SugarContext.init(instance); //orm数据库初始化
        //SugarContext.init(new SugarContextWrapper(this, getDbPath()));  //数据库保存指定路径下
        if (!MLog.DEBUG_LOG) {
            CrashHandlerUtil.getInstance().init(instance);// 错误日志搜集
        }
        initComponent(preferenceSourceClass, baseApiSource, baseUrl);
        Settings setting = Logger.init("api");
        setting.logLevel(MLog.DEBUG_LOG ? LogLevel.FULL : LogLevel.NONE) //  显示全部日志，LogLevel.NONE不显示日志，默认是Full
//                .methodCount(5)         //  方法栈打印的个数，默认是2
                .methodOffset(0)        //  设置调用堆栈的函数偏移值，0的话则从打印该Log的函数开始输出堆栈信息，默认是0
                .hideThreadInfo();      //  隐藏线程信息

        Density.setDensity(instance, 375f);

        //ARouter 初始化再启动器的 AppInitializer 中 CommonInitializer 中已经初始化了，此处不用再初始化

        MkvUtils.get().init(instance);
        //收集app的使用情况
        lifecycleCallbacks = new ActivityLifeCycle();
        instance.registerActivityLifecycleCallbacks(lifecycleCallbacks);
    }

    public void onTerminate() {
        SugarContext.terminate();  //orm数据库结束
        ARouter.getInstance().destroy();
    }

    private <T> void initComponent(Class<T> preferenceSourceClass, BaseApiSourceImpl baseApiSource, String baseUrl) {
        if (preferenceSourceClass != null) {
            appComponent = DaggerAppComponent.builder()
                    .appModule(new AppModule(instance, preferenceSourceClass))
                    .networkModule(new NetworkModule(baseApiSource, baseUrl))
                    .build();
        } else {
            appComponent = DaggerAppComponent.builder()
                    .appModule(new AppModule(instance, getPreferenceSourceClass()))
                    .networkModule(new NetworkModule(getApiSoure(), getBaseUrl()))
                    .build();
        }
    }

    public AppComponent getAppComponent() {
        return appComponent;
    }


    protected BaseApiSourceImpl getApiSoure() {
        return new BaseApiSourceImpl();
    }

    protected String getBaseUrl() {
        return BuildConfig.BASE_URL;
    }

    protected String getDbPath() {
        return null;
    }

    protected <T> Class<T> getPreferenceSourceClass() {
        return (Class<T>) BasePreferenceSource.class;
    }

}
